<?php
function executeFunction($functionName, $parameters) {
    try {
        if (defined('TMSVC_MYSQL_HOST')) {
            $conn = mysql_connect(TMSVC_MYSQL_HOST, TMSVC_MYSQL_USERNAME, TMSVC_MYSQL_PASSWORD);
            mysql_select_db(TMSVC_MYSQL_DATABASE, $conn);
        }
        $result = include TMSVC_PATH_APP.'/'.strtolower($functionName).'.php';
        if (defined('TMSVC_MYSQL_HOST')) {
            mysql_close($conn);
        }
    }
    catch (Exception $e) {
        return new nusoap_fault($e->getCode(), $e->getFile().':'.$e->getLine(), $e->getMessage(), $e->getTraceAsString());
    }
    return $result;
}

if (!file_exists(TMSVC_PATH_TMP.'/'.TMSVC_NAME2.'.class.php')) {
    function registerClass($dir) {
        $ret = '';
        $dir_handle = opendir($dir);
        while ($dir_content = readdir($dir_handle)) {
            if ($dir_content{0} == '.') continue; // we do not parse hidden files
            if (is_dir($dir.'/'.$dir_content))
                 $ret.= registerClass($dir.'/'.$dir_content);
            else {
                unset($info);
                include $dir.'/'.$dir_content;
                $f_params = array();
                $e_params = array();
                foreach ($info['in'] as $i_key => $i_val) {
                    $f_params[] = '$'.$i_key;
                    $e_params[] = '\''.$i_key.'\' => $'.$i_key;
                }
                $ret.= ' function '.$info['function'].'('.implode(',', $f_params).') { return executeFunction(\''.$info['function'].'\', array('.implode(',', $e_params).')); } ';
            }
        }
        closedir($dir_handle);
        return $ret;
    }
    // generate class
    $class_code = '<?php '; //'<?php class '.TMSVC_NAME2.' { ';
    $class_code.= registerClass(TMSVC_PATH_INFO);
    //$class_code.= ' }';
    file_put_contents(TMSVC_PATH_TMP.'/'.TMSVC_NAME2.'.class.php', $class_code);
}
include TMSVC_PATH_TMP.'/'.TMSVC_NAME2.'.class.php';

